function threeApp(c) {
    if (!c.prize) {
        return
    }
    this.hadou = c.hadou || 0;
    this.bttn = c.bttn || 20;
    //this.remainChance = c.remainChance || 0;
	this.init(c.prize);
    this.url = c.url;
    this.callback = c.callback;
    var d = (/hp-tablet/gi).test(navigator.appVersion);
    var a = "ontouchstart" in window && !d;
    this.MOVE_EV = a ? "touchmove": "mousemove";
    this.START_EV = a ? "touchstart": "mousedown";
    this.END_EV = a ? "touchend": "mouseup";
    this.CANCEL_EV = a ? "touchcancel": "mouseout";
    this.eventFlag = false;
    this.threeVal = -1;
    this.gint = [null, null, null];
    this.setEndClick(false);
    this.setStartClick(true);
    this.step = 0;
    this.time = 0;
    this.stoped = 0;
    this.bw = -1;
    this.sw = -1;
    this.gw = -1;
    this.prizedou = 0;
    this.awards = 'nowing';
}
threeApp.prototype = {
    init: function(b) {
        var d = this,
        a = "";
        d.iSize = b.length;
        for (var c = 0; c < d.iSize; c += 1) {
            a += "<img src='" + b[c] + "' alt=''>"
        }
        a += "<img src='" + b[0] + "' alt=''><img src='" + b[1] + "' alt=''>";
        $("#three-area").html("<li>" + a + "</li><li>" + a + "</li><li>" + a + "</li>");
        this.prizeHeight = $($("#three-area").find("img")[0]).height();
        d.sHeight = (d.iSize + 1) * d.prizeHeight;
        d.iLi = $("#three-div .scroll-area li");
        d.iLi.css("height", this.sHeight + this.prizeHeight * 2);
//        d.iLi.css("height", this.sHeight);
    },
    start: function() {
        var b = this;
        b.setStartClick(false);
        if(this.step==0){
            /*if (b.remainChance < 1) { 
				$(".chanceTips").html('今天的机会用完了');
				b.setStartClick(true);
				b.setEndClick(false);
				$('#start-btn').css({'display':'block'});
				alert('今天的机会用完了');					
                return;
            }
            */
			if(b.bttn>b.hadou){
				$(".chanceTips").html(alertunit+'不足');
				b.setStartClick(true);
				b.setEndClick(false);
				$('#start-btn').css({'display':'block'});
				eval("x0p(alertunit+'不足', '本游戏每次需要100'+alertunit, 'warning');");
//				alert(alertunit+'不足');
				return;				
			}
            try{
                document.getElementById("startThree").currentTime = 0;
                document.getElementById("stopOne").currentTime = 0;
                document.getElementById("getHadou").currentTime = 0;
            }catch(err){}
            $("#startThree")[0].play();        
            b.hadou = b.hadou-b.bttn;
            $("#tips").text('-'+b.bttn).animate({'margin-top':'-10px','font-size':'20px'},1000,function(){
                $("#tips").text('').animate({'margin-top':'0px','font-size':'15px'});
            });			
            $("#hadou").text(b.hadou);           

            for (var a = 0; a < 3; a++) { (function(c) {
					b.scrollFn(c)
				})(a)
            }

            b.step = 1;
            b.time = new Date().getTime();
			b.stoped=0;
            b.getVal();
			$('#end-btnbox').css({'display':'block'});
			//b.setStop();
        }else{
			b.stop();           
        }      
        $("body").bind(this.MOVE_EV,function(c) {
            c.preventDefault()
        });        
    },  
    setStop: function(){
        var b = this;
        var t = new Date().getTime();
        if(t-b.time>6800){
            b.stop();           
        }else{
            if (b.gint != [null, null, null]){
				if(b.stoped==0){
					setTimeout(function(){
						b.setStop();   
					},10);
				}
			}            
        }        
    },
    stop: function() {
        var a = this;
        a.stoped=1;
		a.setEndClick(false);
		if(a.gint[0] != null){
			setTimeout(function() {
				a.over(0);				
			},12);
		}
		if(a.gint[1] != null){
			setTimeout(function() {
				a.over(1);						
			},678);
		}
		if(a.gint[2] != null){
			setTimeout(function() {
				a.over(2);								
			},1234);
		}		
    },
    scrollFn: function(a) {
        var c = this;
        var b = 0;
        if (!this.gint[a]) {
            this.gint[a] = setInterval(function() {
                b = b + 10 + Math.ceil(Math.random()*50);
                $(c.iLi[a]).css("margin-top", "-" + b + "px");
                if (b > c.sHeight) {
                    $(c.iLi[a]).css("margin-top", -c.prizeHeight + "px");
                    b = c.prizeHeight
                }
            },
            15)
        }
    },
    over: function(b) {
        var d = this;
		clearInterval(d.gint[b]);
		d.gint[b] = null;
		$("#end"+b).removeClass('end-down').removeClass('end-up').addClass('end-ended').unbind();

		var showVal = 0;
		if(b==0){ //百位
			showVal = d.bw;
		}else if(b==1){ //十位
			showVal = d.sw;                 
		}else if(b==2){ //个位
			showVal = d.gw;                  
		}		
		var a = 0-( showVal * (d.prizeHeight+24));	//加24是因为img的css的padding属性	
				
		$(d.iLi[b]).css("margin-top",a+'px');
		try{
            document.getElementById("stopOne").currentTime = 0;
		}catch(err){}
		$("#stopOne")[0].play(); 
        if (d.gint[0] == null && d.gint[1] == null && d.gint[2] == null){                              
			$("#startThree")[0].pause();
			d.step = 0;

			if (d.prizedou>0){
				$("#back").show();
				$("#ghadou").text(d.prizedou);
				setTimeout(function() {
					$(".result").show();
				},1000);
			}
			d.setEndClick(false);
			setTimeout(function() {
				$('#end-btnbox').css({'display':'none'});
				$('#start-btn').css({'display':'block'});
				$('.endbtn').removeClass('end-ended').addClass('end-up');				
				d.setStartClick(true);				
			},1000);			
			$("body").unbind(this.MOVE_EV);
        }
    },
    getVal: function() {
        var a = this;
        console.log(a.url);
        $.ajax({
            url: a.url+'&a=getVal',
			cache: false,
            dataType: "json",
            timeout: 10000,
            error: function() {
                alert('网络错误，这次不算');
				//window.location.href = window.location.href;
            },
            success: function(b) {
                if (b.status=='ok') {
					a.hadou = b.hadou;
					//a.remainChance = b.chance;
					a.bw = b.bw;
					a.sw = b.sw;
					a.gw = b.gw;
					a.prizedou = b.prize;
					a.awards = b.awards;
					console.log(a);
					$("#hadoubtn").attr('data-id',b.hadou);
//					$(".chanceTips").html('今天还有'+b.chance+'次机会');
					
					a.setEndClick(true);
                } else { 
                    alert(b.msg);
					if(b.url != ''){
						window.location.href = b.url;
					}
                }
            }
        })
    },
    setStartClick: function(a) {
        var b = this;
        if (a) {
            $('#start-btn').bind(b.START_EV,
            function() {
                $(this).removeClass('start-up').addClass('start-down');               
                b.eventFlag = true           
            });
            $('#start-btn').bind(b.MOVE_EV,
            function(c) {
                c.preventDefault()
            });
            $('#start-btn').bind(b.END_EV,
            function() {
                if (b.eventFlag) {
                    b.eventFlag = false;
                    $(this).removeClass('start-down').addClass('start-up').css({'display':'none'});                  
                    b.start();
                }
            });
            $('#start-btn').bind(b.CANCEL_EV,
            function() {
                if (b.eventFlag) {
                    b.eventFlag = false;
                    $(this).removeClass('start-down').addClass('start-up').css({'display':'none'});                  
                    b.start();
                }
            })
        } else {
            $('#start-btn').unbind();
        }
    },
    setEndClick: function(a) {
        var b = this;
        if (a) {
            $('.endbtn').bind(b.START_EV,
            function() {
                $(this).removeClass('end-up').addClass('end-down');               
                b.eventFlag = true           
            });
            $('.endbtn').bind(b.MOVE_EV,
            function(c) {
                c.preventDefault()
            });
            $('.endbtn').bind(b.END_EV,
            function() {
                if (b.eventFlag) {
                    b.eventFlag = false;
                    $(this).removeClass('end-down').addClass('end-up');
					var id = $(this).attr('data-id');				
                    b.over(id)
                }
            });
            $('.endbtn').bind(b.CANCEL_EV,
            function() {
                if (b.eventFlag) {
                    b.eventFlag = false;
                    $(this).removeClass('end-down').addClass('end-up');
					var id = $(this).attr('data-id');				
                    b.over(id)
                }
            });
        } else {
            $('.endbtn').unbind();
        }
    }	
};